package com.example.demo.controller;

import com.example.demo.domain.Curriculum;
import com.example.demo.service.CurriculumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/pri/curriculum")
public class CurriculumController {

    private CurriculumService curriculumService;

    @Autowired
    public void setCurriculumService(CurriculumService curriculumService){
        this.curriculumService = curriculumService;
    }

    /**
     * 查询所有课程
     * @param model
     * @return
     */
    @GetMapping("/findAll")
    public String findAll(@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                          @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize,
                          Model model){
        Integer totalPage = null;
        pageNum = (pageNum-1)*pageSize;
        List<Curriculum> curriculumList = curriculumService.findAll(pageNum,pageSize);
        Integer pageTotal = curriculumService.findTotalRecord();
        model.addAttribute("curriculumList",curriculumList);
        //实行分页
        if(pageTotal % pageSize == 0){
            totalPage = pageTotal / pageSize;
        }else{
            totalPage = pageTotal / pageSize + 1;
        }
        model.addAttribute("totalPage",totalPage);
        return "curriculum/list";
    }

    /**
     * 删除课程功能
     * @param request
     * @return
     */
    @GetMapping("/delete")
    public String delete(HttpServletRequest request){
        String courseNo = request.getParameter("courseNo");
        curriculumService.delete(courseNo);
        return "redirect:/pri/curriculum/findAll";
    }

    /**
     * 添加课程功能
     * @param model
     * @return
     */
    @GetMapping("/toAdd")
    public String toAdd(Model model){
        model.addAttribute("curriculum", new Curriculum());
        return "curriculum/add";
    }

    @PostMapping("/add")
    public String add(Curriculum curriculum){
        curriculumService.add(curriculum);
        return "redirect:/pri/curriculum/findAll";
    }

    /**
     * 修改课程信息
     * @param request
     * @param model
     * @return
     */
    @GetMapping("/toUpdate")
    public String toUpdate(HttpServletRequest request, Model model){
        String id = request.getParameter("courseNo");
        Curriculum curriculum = curriculumService.findCourseByNo(id);
        model.addAttribute("curriculum",curriculum);
        return "curriculum/modify";
    }

    @PostMapping("/update")
    public String update(Curriculum curriculum){
        curriculumService.update(curriculum);
        return "redirect:/pri/curriculum/findAll";
    }


}
